//
//  LSZHelpEachOtherVC.m
//  PartyClassCloud
//
//  Created by 梁素珍 on 2018/8/14.
//  Copyright © 2018年 九酷. All rights reserved.
//

#import "LSZHelpEachOtherVC.h"

@interface LSZHelpEachOtherVC ()<UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate>
@property(nonatomic,strong) UITableView * tableViewHelp;
@property(nonatomic,strong) NSMutableArray * mutArrayAllMg;
@property(nonatomic,strong) NSMutableArray * mutArrayMyMg;


@property(nonatomic,strong) NSMutableArray * mutArrayMg;
//@property(nonatomic,assign) NSInteger page;
//@property(nonatomic,assign) NSInteger pageMy;

@property(nonatomic,strong) UIButton * butAll;
@property(nonatomic,strong) UIButton * butMy;
@property(nonatomic,assign) NSInteger chooseWhat;



@end

@implementation LSZHelpEachOtherVC
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden=NO;
    self.navigationController.delegate = self;
    
}
#pragma mark - 获取全部问题
-(void)getHelpData:(NSInteger)page
{
    [LSZRequestDicData initWithMutualProblem:page :^(BOOL isOk, NSString * string, LSZMutualProblemManager * manager) {
        if (isOk == YES) {
            if(page == 0){
                [self.mutArrayAllMg removeAllObjects];
                [self.mutArrayAllMg addObjectsFromArray:manager.mutMgArray];
            }else{
                [self.mutArrayAllMg addObjectsFromArray:manager.mutMgArray];
            }
            self.mutArrayMg = self.mutArrayAllMg;
            [self.tableViewHelp reloadData];
        }else{
            [LSZPublicClassDataToll initWithAlertKuangView:self.view string:string];
//            if ([string isEqualToString:@"重复登录"]&&page==0) {
//                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                    [LSZPublicClassDataToll RepeatLoginInterfaceJump:self.view fromVC:@"LSZHelpEachOtherVC"];
//                });
//            }else{
//                [LSZPublicClassDataToll initWithAlertKuangView:self.view string:string];
//            }
        }
    }];
}
#pragma mark - 获取我的问题
-(void)getMyHelpData:(NSInteger)page
{
    [LSZRequestDicData initWithMutualProblemSelf:page :^(BOOL isOk, NSString * string, LSZMutualProblemManager * manager) {
        if (isOk == YES) {
            if(page == 0){
                [self.mutArrayMyMg removeAllObjects];
                [self.mutArrayMyMg addObjectsFromArray:manager.mutMgArray];
            }else{
                [self.mutArrayMyMg addObjectsFromArray:manager.mutMgArray];
            }
            self.mutArrayMg = self.mutArrayMyMg;
            [self.tableViewHelp reloadData];
        }else{
//            if ([string isEqualToString:@"重复登录"]&&page==0) {
//                [self.view removeFromSuperview];
//                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                    [LSZPublicClassDataToll RepeatLoginInterfaceJump:self.view fromVC:@"LSZHelpEachOtherVC"];
//                });
//            }else{
//                [LSZPublicClassDataToll initWithAlertKuangView:self.view string:string];
//            }
            [LSZPublicClassDataToll initWithAlertKuangView:self.view string:string];

        }
    }];
}
-(void)goToLogin
{
    [LSZPublicClassDataToll goToLogin];
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.navigationItem.title = @"互助";
//    NSString * strDelete = [NSString stringWithFormat:@"%@Login",@"LSZHelpEachOtherVC"];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(goToLogin) name:strDelete object:nil];
    
    [self addTitleBut];
//    self.page = 0;
//    self.pageMy = 0;
    self.mutArrayMg = [[NSMutableArray alloc]init];
    self.mutArrayAllMg = [[NSMutableArray alloc]init];
    self.mutArrayMyMg = [[NSMutableArray alloc]init];
 
    __weak typeof(self) weakSelf = self;
    weakSelf.tableViewHelp = [[UITableView alloc]initWithFrame:CGRectMake(0,NAVIGATION_BAR_HEIGHT, ScreenWidth, ScreenHeight-NAVIGATION_BAR_HEIGHT) style:UITableViewStyleGrouped];
    weakSelf.tableViewHelp.delegate = self;
    weakSelf.tableViewHelp.dataSource = self;
    weakSelf.tableViewHelp.estimatedRowHeight=0;
    weakSelf.tableViewHelp.estimatedSectionFooterHeight =0;
    weakSelf.tableViewHelp.estimatedSectionHeaderHeight =0;
    weakSelf.tableViewHelp.showsVerticalScrollIndicator = NO;
    [weakSelf.tableViewHelp setSeparatorInset:UIEdgeInsetsMake(0, 20, 0, 20)];
    [weakSelf.view addSubview:self.tableViewHelp];
    [weakSelf.tableViewHelp registerClass:[LSZHelpTableViewCell class] forCellReuseIdentifier:@"LSZHelpTableViewCell"];
    [weakSelf.tableViewHelp registerClass:[LSZHeadViewCell class] forHeaderFooterViewReuseIdentifier:@"LSZHeadViewCell"];

    [weakSelf getHelpData:0];
//    [weakSelf.tableViewHelp addRefreshHeaderWithHandle:^{
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            // 刷新表格  和结束刷新
//            self.page = 0;
//            [weakSelf getHelpData:self.page];
//            [weakSelf.tableViewHelp.mj_header endRefreshing];
//        });
//    }];
    
    weakSelf.tableViewHelp.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            self.page = 0;
            if (self.chooseWhat == 1) {
                [weakSelf getHelpData:0];
            }else{
//                self.pageMy = 0;
                [weakSelf getMyHelpData:0];
            }
            [weakSelf.tableViewHelp.mj_header endRefreshing];
        });
    }];
//    weakSelf.tableViewHelp.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        //延时
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            self.page++;
//            if (self.chooseWhat == 1) {
//                [self getHelpData:self.page];
//            }else{
//                self.pageMy++;
//                [weakSelf getMyHelpData:self.pageMy];
//            }
//            [self.tableViewHelp.mj_footer endRefreshing];
//        });
//    }];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(InfoNotificationMutualAid:) name:@"mutualAid" object:nil];
}
#pragma mark - 状态改变的通知state2变已解决  1删除
-(void)InfoNotificationMutualAid:(NSNotification *)notification
{
    NSDictionary * dic = notification.userInfo;
    if ([NullStringjudgment(NSStr(dic[@"chooseWhat"])) isEqualToString:@"1"]) {
         //全部
        NSInteger number = [dic[@"number"] integerValue];//第几个
        if([NullStringjudgment(NSStr(dic[@"state"])) isEqualToString:@"2"]){
            LSZMutualProblemMg * manager = nil;
            manager = self.mutArrayAllMg[number];
            manager.solved = @"1";
        }else{
            [self.mutArrayAllMg removeObjectAtIndex:number];
        }
        [self.tableViewHelp reloadData];
    }else{
        NSInteger number = [dic[@"number"] integerValue];//第几个
        if([NullStringjudgment(NSStr(dic[@"state"])) isEqualToString:@"2"]){
            LSZMutualProblemMg * manager = nil;
            manager = self.mutArrayMyMg[number];
            manager.solved = @"1";
        }else{
            [self.mutArrayMyMg removeObjectAtIndex:number];
        }
        [self.tableViewHelp reloadData];
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.mutArrayMg.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 210;
    }else{
        return 10;
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        LSZHeadViewCell * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"LSZHeadViewCell"];
        cell.imageViewHeader.image = [UIImage imageNamed:@"互助banner"];
        return cell;
    }else{
        UITableViewHeaderFooterView * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:@"cell"];
        }
        return cell;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSZHelpTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZHelpTableViewCell" forIndexPath:indexPath];
    if (self.mutArrayMg.count > indexPath.row) {
        LSZMutualProblemMg * mg = nil;
        mg = self.mutArrayMg[indexPath.section];
        cell.mutualProblemManager = mg;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.mutArrayMg.count > indexPath.row) {
        LSZMutualProblemMg * mg = nil;
        mg = self.mutArrayMg[indexPath.section];
        LSZDetailsProblemVC * vc = [[LSZDetailsProblemVC alloc]init];
        vc.id_id = mg.id_id;
        vc.mutualProblemMg = mg;
        vc.numRow = indexPath.row;
        vc.chooseWhat = self.chooseWhat;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
#pragma mark - 点击发布按钮
-(void)clickAdd
{
    LSZPostQuestionsVC * VC = [[LSZPostQuestionsVC alloc]init];
    [self.navigationController pushViewController:VC animated:YES];
}
-(void)clickButAll:(UIButton *)button
{
    if (self.chooseWhat == 1 && button.tag == 1) {
        return;
    }
    if (self.chooseWhat == 2 && button.tag == 2) {
        return;
    }
    if (button.tag == 1) {
        self.chooseWhat = 1;
        self.butAll.backgroundColor = themeColor;
        self.butMy.backgroundColor = [UIColor whiteColor];
        [self.butAll setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.butMy setTitleColor:titleColorLSZ forState:UIControlStateNormal];
        if (self.mutArrayAllMg.count == 0) {
//            self.page = 0;
            [self getHelpData:0];
        }else{
            self.mutArrayMg = self.mutArrayAllMg;
            [self.tableViewHelp reloadData];
        }
    }else{
        self.chooseWhat = 2;
        self.butAll.backgroundColor = [UIColor whiteColor];
        self.butMy.backgroundColor = themeColor;
        [self.butAll setTitleColor:titleColorLSZ forState:UIControlStateNormal];
        [self.butMy setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        if (self.mutArrayMyMg.count == 0) {
//            self.pageMy = 0 ;
            [self getMyHelpData:0];
        }else{
            self.mutArrayMg = self.mutArrayMyMg;
            [self.tableViewHelp reloadData];
        }
    }
}
-(void)addTitleBut
{
    self.chooseWhat = 1;
    UIView * viewTop = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, NAVIGATION_BAR_HEIGHT)];
    viewTop.backgroundColor = RGBColor(236, 236, 236);
    [self.view addSubview:viewTop];
    CGFloat size;
    CGFloat A;
    if (iPhoneX) {
        size = 44;
        A = 10;
    }else{
        size = 40;
        A = 5;
    }
    UIView * viewChoose = [[UIView alloc]initWithFrame:CGRectMake((ScreenWidth-200)/2, NAVIGATION_BAR_HEIGHT-size-A, 200, size)];
    viewChoose.backgroundColor = [UIColor whiteColor];
    [viewChoose.layer setMasksToBounds:YES];
    [viewChoose.layer setCornerRadius:4];
    [viewTop addSubview:viewChoose];
    
    self.butAll = [[UIButton alloc]init];
    self.butAll.frame = CGRectMake(0, 0,viewChoose.frame.size.width/2, viewChoose.frame.size.height);
    [self.butAll setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.butAll.backgroundColor = themeColor;
    [self.butAll setTitle:@"全部" forState:UIControlStateNormal];
    self.butAll.tag = 1;
    [self.butAll addTarget:self action:@selector(clickButAll:) forControlEvents:UIControlEventTouchUpInside];
    [viewChoose addSubview:self.butAll];
    
    self.butMy = [[UIButton alloc]init];
    self.butMy.frame = CGRectMake(CGRectGetMaxX(self.butAll.frame), 0,viewChoose.frame.size.width/2, viewChoose.frame.size.height);
    [self.butMy setTitleColor:titleColorLSZ forState:UIControlStateNormal];
    self.butMy.backgroundColor = [UIColor whiteColor];
    [self.butMy setTitle:@"我的" forState:UIControlStateNormal];
    self.butMy.tag = 2;
    [self.butMy addTarget:self action:@selector(clickButAll:) forControlEvents:UIControlEventTouchUpInside];
    [viewChoose addSubview:self.butMy];
    
    UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
    but.frame =CGRectMake(ScreenWidth-33-A-5,CGRectGetMinY(viewChoose.frame)+(viewChoose.frame.size.height-33)/2, 33, 33);
    [but setImage:[UIImage imageNamed:@"笔"] forState:UIControlStateNormal];
    [but addTarget:self action:@selector(clickAdd) forControlEvents:UIControlEventTouchUpInside];
    [viewTop addSubview:but];
}
- (void)navigationController:(UINavigationController*)navigationController willShowViewController:(UIViewController*)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}
@end
